'''
  @Description      
  @auther         leizi
  @create          2020-02-09 18:47
'''
import os, time, platform, subprocess


def getsystemsta():  # 获取系统的名称，使用对应的指令
    system = platform.system()
    if system == 'Windows':
        find_manage = 'findstr'
    else:
        find_manage = 'grep'
    return find_manage


def starttime_app(packagename, packagenameactivicy):  # 启动耗时
    cmd = 'adb shell am start -W -n %s' % packagenameactivicy
    me = os.popen(cmd).read().split('\n')[-7].split(':')  # 获取启动时间
    cmd2 = 'adb shell am force-stop %s' % packagename
    os.system(cmd2)
    return me


def liulangone(packagename):
    cmd = 'adb shell ps  | %s %s' % (getsystemsta(), packagename)
    cm = os.popen(cmd).read().split()[1]
    cmd1 = "adb shell cat /proc/" + cm + "/net/dev | findstr wlan0"
    liul = os.popen(cmd1).read().split()
    me1_recive = liul[1]  # 接受
    me2_sned = liul[9]  # 上传
    return me1_recive, me2_sned


def getliuliangto(packagename, me1_recive, me2_sned):
    # 流量
    cmd = 'adb shell ps  | %s %s' % (getsystemsta(), packagename)
    cm = os.popen(cmd).read().split()[1]
    cmd2 = "adb shell cat /proc/" + cm + "/net/dev | findstr wlan0"
    liul1 = os.popen(cmd2).read().split()
    me2_recive = liul1[1]  # 接受
    me2_send = liul1[9]  # 上传
    me1 = int(int(me2_recive) - int(me1_recive)) / 1024
    me2 = int(int(me2_send) - int(me2_sned)) / 1024
    liulang_sum = me1 + me2
    return me1, me2, liulang_sum


def simu_network(packagename, device_name):
    '''
    获取模拟器的流量
    获取上传和下载的流量
    :return:
    '''
    total = ''

    cmd = 'adb shell ps  | %s %s' % (getsystemsta(), packagename)
    cm = os.popen(cmd).read().split()[1]
    cmd = 'adb -s %s shell cat /proc/%s/net/dev' % (device_name, cm)
    # 获取流量命令
    pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
    for index in pipe.readlines():
        # if index.startswith(' wlan0'): # 真机
        if 'eth0' in index:  # 模拟器
            down = index.split()[1]
            # 下载
            send = index.split()[9]
            # 上传
            total = (int(down) + int(send)) / (1024 * 1024)
            # 上传和下载


def caijicpu(packagename):
    # 这里采集的cpu时候可以是执行操作采集 就是-n  -d  刷新间隔
    cpu = 'adb shell top -n 1| %s %s' % (getsystemsta(), packagename)
    re_cpu = os.popen(cpu).read().split()[2]
    return re_cpu


def getnencun(packagename):
    # Total 的实际使用过物理内存
    cpu = 'adb shell top -n 1| %s %s' % (getsystemsta(), packagename)
    re_cpu = int(os.popen(cpu).read().split()[8][:-1]) / 1024
    return re_cpu
